﻿using Aspose.Html;
using Aspose.Html.Converters;
using Aspose.Html.Drawing;
using Aspose.Html.Saving;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataDoc.App.Helper
{
    public class DocPdfHelper
    {
        public DocPdfHelper() { }

        /// <summary>
        /// 创建PDF文件
        /// </summary>
        /// <param name="htmlPath">HTML文件路径</param>
        /// <param name="height">生成PDF的高度</param>
        public static void CreatePDF(string htmlPath, int height)
        {
            try
            {
                string docPath = Application.StartupPath + "\\doc\\";
                if (!Directory.Exists(docPath))
                {
                    Directory.CreateDirectory(docPath);
                }
                string pdfPath = docPath + "\\data_design_pdf_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".pdf";
                var document = new HTMLDocument(htmlPath);
                // 设置页面大小、边距、分辨率及背景颜色 
                var options = new PdfSaveOptions()
                {
                    HorizontalResolution = 200,
                    VerticalResolution = 200,
                    JpegQuality = 100,
                    BackgroundColor = System.Drawing.Color.White
                };
                options.PageSetup.AnyPage = new Page(new Aspose.Html.Drawing.Size(1600, height), new Margin(20, 10, 10, 10));
                // 将HTML转成PDF
                Converter.ConvertHTML(document, options, pdfPath);
            }
            catch (Exception ex)
            {
                Serilog.Log.Error(ex, "CreatePDF");
            }
        }
    }
}
